package com.hottv.meeting.videomeeting.module.white;

import android.content.ClipData;

import com.herewhite.sdk.domain.PptPage;
import com.herewhite.sdk.domain.Scene;
import android.util.Log;

import com.google.gson.JsonObject;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.white.data.DocumentInfo;
import com.hottv.meeting.videomeeting.module.white.data.ItemInfo;

import org.json.JSONObject;

/**
 * Created by chengwenqiang on 2019/6/3.
 */

public class WhiteManager {

    private String DOCUMENT_HOST = "http://fileyidonghottv.bmobpay.com/";

    private DocumentInfo mDocument;

    private int mCurrent = 0;

    private WhiteManager()
    {}

    private static  WhiteManager instance;

    public static WhiteManager getInstance()
    {
        if(instance==null)
        {
            instance = new WhiteManager();
        }
        return instance;
    }

    public void addDocumentJson(JsonObject json)
    {
        mDocument = DocumentInfo.fromJson(json);
    }

    public DocumentInfo getmDocumentInfo()
    {
        return  mDocument;
    }

    public int getDocumentPercentage()
    {
        if(mDocument!=null)
          return   mDocument.convertedPercentage;
        return  0;
    }

    public ItemInfo getItemInfo(int current)
    {
        if(mDocument!=null)
        {
            if(mDocument.itemList.size()>current)
            {
                return mDocument.itemList.get(current);
            }
            else
            {
                Log.e(MeetingConfig.TAG,"current is big then size()");
            }
        }
        return  null;
    }

    //获取当前页序号
    public int getCurrentPage()
    {
        return  mCurrent;
    }

    public int getTotalPage()
    {
        if(mDocument!=null)
            return  mDocument.totalPageSize;
        return 0;
    }

    //获取当前页信息
    public ItemInfo getCurrentPageInfo()
    {
        return  getItemInfo(mCurrent);
    }

    //获取当前页host
    public String getCurrentPageHost()
    {
         ItemInfo info = getCurrentPageInfo();
         if(info!=null)
             return DOCUMENT_HOST +info.conversionFileUrl;
         return "";
    }

    public int getNextPageCur()
    {
        if(mDocument!=null)
        {
            if(mCurrent<mDocument.itemList.size()-1)
            {
                mCurrent = mCurrent +1;
                return  mCurrent;
            }
        }
        return -1;
    }



    //获取前一页面信息
    public int getBeforePageCur()
    {
        if(mCurrent>0)
        {
            mCurrent = mCurrent -1;
            return  mCurrent;
        }

        return -1;
    }



    //重置当前页
    public void resetCurrentPage()
    {
        mCurrent = 0;
    }

    public boolean checkProcess()
    {
        if(mDocument!=null)
        {
            if(mCurrent>mDocument.convertedPageSize)
                return true;
        }
        return false;
    }



    private int mConvertedPage = 0;
    public Scene[] getInitScene()
    {
        mConvertedPage = 0;
        if(mDocument.convertedPageSize>0&& mDocument.convertedPageSize<mDocument.totalPageSize)
        {
            mConvertedPage = mDocument.convertedPageSize;
            Scene scenes[] = new Scene[mConvertedPage];
            Log.i(MeetingConfig.TAG,"getInitScene num:"+mConvertedPage);
            for(int i=0;i<mConvertedPage;i++)
            {
                double width =(double)mDocument.itemList.get(i).height;
                double heigh = (double) mDocument.itemList.get(i).width;
                String host = DOCUMENT_HOST +mDocument.itemList.get(i).conversionFileUrl;

                //调整长宽比例
//                if(width>0&&heigh>0)
//                {
//                    if(width/heigh > MeetingConfig.ppt_width/MeetingConfig.ppt_heigh)
//                    {
//                        heigh = (heigh*MeetingConfig.ppt_width)/width;
//                        width = MeetingConfig.ppt_width;
//
//                    }
//                    else
//                    {
//                        width = width*MeetingConfig.ppt_heigh/heigh;
//                        heigh = MeetingConfig.ppt_heigh;
//                    }
//                }
                Log.i(MeetingConfig.TAG,"host:"+host+",width:"+width+",heigh:"+heigh);
                scenes[i] =     new Scene(""+String.valueOf(i), new PptPage(host, (double)width, (double)heigh));
            }
            return scenes;
        }
        return  null;
    }

    public Scene[] getLeftScene()
    {
        if(mDocument.convertedPageSize>0)
        {
            int leftNum = mDocument.totalPageSize-mConvertedPage;
            Scene scenes[] = new Scene[leftNum];
            Log.i(MeetingConfig.TAG,"getLeftScene num:"+leftNum);
            for(int i=mConvertedPage;i<mDocument.totalPageSize;i++)
            {
                double width =mDocument.itemList.get(i).height ;
                double heigh = mDocument.itemList.get(i).width;
//                if(width>0&&heigh>0)
//                {
//                    if((width/heigh) < (MeetingConfig.ppt_width/MeetingConfig.ppt_heigh))
//                    {
//                        heigh = (heigh*MeetingConfig.ppt_width)/width;
//                        width = MeetingConfig.ppt_width;
//                    }
//                    else
//                    {
//                        width = width*MeetingConfig.ppt_heigh/heigh;
//                        heigh = MeetingConfig.ppt_heigh;
//                    }
//                }
                String host = DOCUMENT_HOST +mDocument.itemList.get(i).conversionFileUrl;
                Log.i(MeetingConfig.TAG,"host:"+host+",width:"+width+",heigh:"+heigh);
                scenes[i-mConvertedPage] =     new Scene(""+String.valueOf(i), new PptPage(host, (double)width, (double)heigh));
            }
            mConvertedPage = mDocument.totalPageSize;
            return scenes;
        }
        return  null;
    }
}
